package cn.edu.nuc.fourth.controller;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.nuc.fourth.dto.PageInfo;
import cn.edu.nuc.fourth.model.Teacher;
import cn.edu.nuc.fourth.services.interfaces.FillingService;

@Controller
@RequestMapping("/teacher")
public class TeacherController {
	@Autowired
	private FillingService service;

	@RequestMapping("/forteacheradd")
	public String forteacheradd(HttpSession session, Model model) {

		/*teacher teacher = service.noAddteacher(-1);
		if (teacher != null) {
			model.addAttribute("teacher", teacher);
			if (teacher.getState() == 1) {

				return "teacher/noeditteacher";
			}
			return "teacher/oneteacher";
		}*/
		return "teacher/teacherAdd";
	}

	@RequestMapping(value = "/teacheradd", method = RequestMethod.POST)
	public String teacheradd(@Valid @ModelAttribute("teacher") Teacher teacher, Errors errors) {

		if (errors.hasErrors()) {
			System.out.println("校验学生信息填报");
			return "teacher/teacherAdd";
		}
		service.teacheradd(teacher);
		return "OK";
	}

	@RequestMapping("/teacherlist")
	public String teacherlist(Integer userid, @RequestParam(name = "current", defaultValue = "1") Integer current,
			Model model) {

		PageInfo pageInfo = service.teacherlist(-1, current);
		model.addAttribute("page", pageInfo);
		return "teacher/teacher";
	}
/*
	@RequestMapping("/oneteacher")
	public String oneteacher(Integer teacherid, Model model) {

		teacher teacher = service.oneteacher(teacherid);
		model.addAttribute("teacher", teacher);
		model.addAttribute("s", "false");
		return "teacher/oneteacher";
	}

	@RequestMapping("/teacheredit")
	public String teacheredit(teacher teacher, Model model) {

		teacher s = service.teacheredit(teacher);
		model.addAttribute("teacher", s);
		model.addAttribute("s", "true");
		return "teacher/oneteacher";
	}

	@RequestMapping("/noeditteacher")
	public String noeditteacher(Integer teacherid, Integer state, Model model) {

		teacher s = service.noeditteacher(teacherid, state);
		model.addAttribute("teacher", s);
		return "teacher/noeditteacher";
	}*/
}
